home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Sound / DelfinaMPEG / src / delfinampeg.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-07  |  22.9 KB  |  658 lines

  1. /*****************************************************************************
  2.  
  3.     delfinampeg.device - mpeg.device for Delfina DSP
  4.     Copyright (C) 2000, 2001  Michael Henke
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20. *****************************************************************************/
  21.  
  22.  
  23. /*#define DEBUG*/
  24.  
  25. #include <proto/exec.h>
  26. #include <proto/dos.h>
  27. #include <exec/memory.h>
  28. #include <exec/devices.h>
  29. #include <exec/errors.h>
  30. #include <exec/interrupts.h>
  31. #include <devices/melodympeg.h>
  32. #include <dos/var.h>
  33.  
  34. #ifdef DEBUG
  35. extern void KPutStr(STRPTR);
  36. extern LONG KPutChar(LONG);
  37. #endif
  38.  
  39. extern struct DelfObj DSP56K_PCM;
  40. extern struct DelfObj DSP56K_MP2;
  41. extern struct DelfObj DSP56K_MP3;
  42. #include <libraries/delfina.h>
  43. #include "PCM.h"
  44. #include "MP2.h"
  45. #include "MP3.h"
  46.  
  47. /** static LONG pow43tab[8206]; **/
  48. #include "MP3_pow43tab.h"
  49. /** static LONG quantab[512]; **/
  50. #include "MP3_quantab.h"
  51.  
  52. extern char DevName;    /* "delfinampeg.device" */
  53. extern struct Library *DelfinaBase; /* initialized by device Init routine */
  54.  
  55. struct devunit
  56. {
  57.     struct Unit unit;
  58.     ULONG   unitnum;
  59.     struct List ioreqlist;
  60.     struct IOMPEGReq *currioreq;
  61.     UBYTE   *currpt;
  62.     ULONG   currlen;
  63.     ULONG   volumeleft, volumeright;
  64.     ULONG   layer, freqidx, mono, firstheader;
  65.     ULONG   initdelf_ok, pause;
  66.     struct DelfPrg *prg_pcm, *prg_mp2, *prg_mp3;
  67.     struct DelfModule *mod_pcm;
  68.     DELFPTR mem_il_mp2, mem_ip_mp2;
  69.     DELFPTR mem_il_mp3, mem_ip_mp3, mem_x_mp3_ro, mem_y_mp3_quantab;
  70.     struct Interrupt delfint;
  71.     ULONG   intkey;
  72.     UBYTE   framebuf[4096], bitresbuf[4096];
  73.     UWORD   bitresoffset, bitresok, framebufstate;
  74.     UWORD   framebufoffset, framebufleft, III_main_data_size;
  75.     UWORD   delfcopysize, II_translate, II_jsbound, modext;
  76.     UWORD   II_forcemono, III_forcemono, forcemono;
  77.     ULONG   II_dacrate, III_dacrate, dacrate;
  78.     UBYTE   *delfcopypt;
  79. };
  80.  
  81. /* possible values for 'framebufstate' */
  82. #define FBS_GETHEADER           0
  83. #define FBS_GETFRAMEDATA        1
  84. #define FBS_FILLED              2
  85.  
  86. #define MPG_MD_STEREO           0
  87. #define MPG_MD_JOINT_STEREO     1
  88. #define MPG_MD_DUAL_CHANNEL     2
  89. #define MPG_MD_MONO             3
  90. #define HDR_MPEG1               0xfff80000
  91. #define HDR_CONSTANT            0xfffe0c00  /* layer, sampling frequency */
  92.  
  93. void* C_initunit(ULONG unitnum);
  94. ULONG C_expungeunit(struct devunit *unit);
  95. void  C_setpause(struct devunit *unit, struct IOMPEGReq *iomr);
  96. void  C_setvolume(struct devunit *unit, struct IOMPEGReq *iomr);
  97. void  C_flush(struct devunit *unit);
  98. ULONG C_write(struct devunit *unit, struct IOMPEGReq *iomr);
  99. static ULONG initDelfina(struct devunit *unit);
  100. static void  cleanupDelfina(struct devunit *unit);
  101. static void __asm IntServer(register __a1 struct devunit *unit);
  102. static UWORD GetBits(UWORD num);
  103.  
  104. static struct TagItem tagdone={TAG_DONE,0};
  105. static ULONG volumeleft=0x400000, volumeright=0x400000;  /* default: 100% */
  106. static UWORD *gb_pt=NULL, gb_buf=0, gb_num=0;
  107.  
  108. static const ULONG mpgfreq[4]={44100,48000,32000,0};
  109. static const UWORD mpgbitrate[3][16]=
  110.         { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,0}, /* I */
  111.           {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384,0},   /* II */
  112.           {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320,0} }; /* III */
  113. static const UWORD mp2translate[3][2][16] =
  114.         { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,    /* 44100 stereo */
  115.             { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,  /* 44100 mono   */
  116.           { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,    /* 48000 stereo */
  117.             { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,  /* 48000 mono   */
  118.           { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,    /* 32000 stereo */
  119.             { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } }; /* 32000 mono   */
  120. static const UWORD mp2sblimit[4]={27,30,8,12};
  121.  
  122.  
  123.  
  124. /** called by OpenDevice() **/
  125. void* C_initunit(ULONG unitnum)
  126. {
  127.     struct devunit *unit;
  128. #ifdef DEBUG
  129. KPutStr("C_initunit\n");
  130. #endif
  131.     unit=(struct devunit*)AllocVec(sizeof(struct devunit),MEMF_PUBLIC|MEMF_CLEAR);
  132.     if(!unit) return(unit);
  133.     unit->unit.unit_MsgPort.mp_Node.ln_Type=NT_MSGPORT;
  134.     unit->unit.unit_MsgPort.mp_Node.ln_Name= &DevName;
  135.     unit->unit.unit_MsgPort.mp_Flags=PA_IGNORE;
  136.     NewList(&unit->unit.unit_MsgPort.mp_MsgList);
  137.     unit->unitnum=unitnum;
  138.     unit->volumeleft=volumeleft;
  139.     unit->volumeright=volumeright;
  140.     NewList(&unit->ioreqlist);
  141.     {
  142.         UBYTE *varbuf;
  143.         LONG  varlen, args[4]={0,0,0,0};
  144.         struct RDArgs *rdargs, *rdargs2;
  145.         if((DOSBase=(struct DosLibrary*)OpenLibrary("dos.library",37)))
  146.         {
  147.             if((varbuf=AllocVec(4096,MEMF_PUBLIC|MEMF_CLEAR)))
  148.             {
  149.                 if((varlen=GetVar("DELFINAMPEG",varbuf,4096,LV_VAR))>0);
  150.                 {
  151. #ifdef DEBUG
  152. KPutStr("DELFINAMPEG var: '"); KPutStr(varbuf); KPutStr("'\n");
  153. #endif
  154.                     if((rdargs=AllocDosObject(DOS_RDARGS,&tagdone)))
  155.                     {
  156.                         rdargs->RDA_Source.CS_Buffer=varbuf;
  157.                         rdargs->RDA_Source.CS_Length=varlen;
  158.                         rdargs->RDA_Source.CS_CurChr=0;
  159.                         rdargs->RDA_Flags=RDAF_NOPROMPT;
  160.                         rdargs2=ReadArgs("L2MONO/S,L2RATE/K/N,L3MONO/S,L3RATE/K/N",args,rdargs);
  161.                         if(args[0]) unit->II_forcemono=1;
  162.                         if(args[1]) unit->II_dacrate=(*(LONG*)args[1]);
  163.                         if(args[2]) unit->III_forcemono=1;
  164.                         if(args[3]) unit->III_dacrate=(*(LONG*)args[3]);
  165.                         FreeArgs(rdargs2);
  166.                         FreeDosObject(DOS_RDARGS,rdargs);
  167.                     }
  168.                 }
  169.                 FreeVec(varbuf);
  170.             }
  171.             CloseLibrary((struct Library*)DOSBase);
  172.         }
  173.     }
  174.     return(unit);
  175. }
  176.  
  177.  
  178.  
  179. /** called by CloseDevice() **/
  180. ULONG C_expungeunit(struct devunit *unit)
  181. {
  182.     ULONG unitnum=unit->unitnum;
  183. #ifdef DEBUG
  184. KPutStr("C_expungeunit\n");
  185. #endif
  186.     C_flush(unit);
  187.     FreeVec(unit);
  188.     return(unitnum);
  189. }
  190.  
  191.  
  192.  
  193. /** called by MPEGCMD_PAUSE **/
  194. void C_setpause(struct devunit *unit, struct IOMPEGReq *iomr)
  195. {
  196. #ifdef DEBUG
  197. KPutStr("C_setpause\n");
  198. #endif
  199.     unit->pause=iomr->iomr_Arg1;
  200. }
  201.  
  202.  
  203.  
  204. /** called by MPEGCMD_SETAUDIOPARAMS **/
  205. void C_setvolume(struct devunit *unit, struct IOMPEGReq *iomr)
  206. {
  207.     struct MPEGAudioParams *ap;
  208. #ifdef DEBUG
  209. KPutStr("C_setvolume\n");
  210. #endif
  211.     ap=(struct MPEGAudioParams*)iomr->iomr_Req.io_Data;
  212.     if(ap)
  213.     {
  214.         unit->volumeleft =volumeleft =(0x400000*((ap->map_VolumeLeft >>8)&0xff))/0xff;
  215.         unit->volumeright=volumeright=(0x400000*((ap->map_VolumeRight>>8)&0xff))/0xff;
  216.         if(unit->prg_pcm)
  217.         {
  218.             Delf_Run(unit->prg_pcm->prog+PROG_PCM_INIT, 0, 0,
  219.                  (ULONG)(unit->forcemono ? 1 : unit->mono),
  220.                  unit->volumeleft,
  221.                  mpgfreq[unit->freqidx],
  222.                  unit->volumeright );
  223.         }
  224.     }
  225. }
  226.  
  227.  
  228.  
  229. /** called by CMD_FLUSH **/
  230. void C_flush(struct devunit *unit)
  231. {
  232.     struct IOMPEGReq *iomr;
  233. #ifdef DEBUG
  234. KPutStr("C_flush\n");
  235. #endif
  236.     cleanupDelfina(unit);
  237.     unit->currlen=0; unit->currpt=NULL; 
  238.     unit->layer=unit->freqidx=unit->mono=unit->firstheader=0;
  239.     unit->bitresoffset=unit->bitresok=0;
  240.     unit->framebufstate=FBS_GETHEADER;
  241.     if(unit->currioreq)
  242.     {
  243.         unit->currioreq->iomr_Req.io_Error=IOERR_ABORTED;
  244.         ReplyMsg(&unit->currioreq->iomr_Req.io_Message);
  245.         unit->currioreq=NULL;
  246.     }
  247.     for(;;)
  248.     {
  249.         if(!(iomr=(struct IOMPEGReq*)RemHead(&unit->ioreqlist))) break;
  250.         iomr->iomr_Req.io_Error=IOERR_ABORTED;
  251.         ReplyMsg(&iomr->iomr_Req.io_Message);
  252.     }
  253. }
  254.  
  255.  
  256.  
  257. /** called by CMD_WRITE **/
  258. ULONG C_write(struct devunit *unit, struct IOMPEGReq *iomr)
  259. {
  260. #ifdef DEBUG
  261. /*KPutStr("C_write\n");*/
  262. #endif
  263.     if( (iomr->iomr_Req.io_Length==0) ||
  264.         (iomr->iomr_Req.io_Data==NULL) ||
  265.         (iomr->iomr_StreamType!=MPEGSTREAM_AUDIO) ) return(1); /*ERROR*/
  266.     Disable();
  267.     AddTail(&unit->ioreqlist, (struct Node*)iomr);
  268.     Enable();
  269. #ifdef DEBUG
  270. KPutStr("C_write...AddTail\n");
  271. #endif
  272.     /*even if initDelfina() returns an error we must not return it  */
  273.     /*here because the request is already queued in our ioreqlist.  */
  274.     /*(the requests in ioreqlist will be replied by C_flush() later)*/
  275.     if(!unit->initdelf_ok) initDelfina(unit);
  276.     return(0);
  277. }
  278.  
  279.  
  280.  
  281. /** called by C_write() **/
  282. static ULONG initDelfina(struct devunit *unit)
  283. {
  284.     while(!unit->firstheader)
  285.     {
  286.         ULONG header, i;
  287.         UBYTE *pt, *ptmax;
  288. #ifdef DEBUG
  289. KPutStr("initDelfina...firstheader\n");
  290. #endif
  291.         /** find frame header and extract some info: layer, freqidx, mono **/
  292.         if(!unit->currioreq)
  293.         {
  294.             if(!(unit->currioreq=(struct IOMPEGReq*)RemHead(&unit->ioreqlist))) return(1); /*ERROR*/
  295.             unit->currpt=unit->currioreq->iomr_Req.io_Data;
  296.             unit->currlen=unit->currioreq->iomr_Req.io_Length;
  297.         }
  298.         pt=unit->currpt; ptmax=pt+unit->currlen; header=0;
  299. retry_firstheader:
  300.         i=0;
  301.         while(pt<ptmax)
  302.         {
  303.             header=(header<<8)|(ULONG)(*pt++);
  304.             if( ((header&HDR_MPEG1)==HDR_MPEG1) &&  /*sync*/
  305.                 (((header>>17)&3)!=0) &&            /*layer!=4*/
  306.                 (((header>>17)&3)!=3) &&            /*layer!=1*/
  307.                 (((header>>12)&15)!=0) &&           /*bitrate!=0*/
  308.                 (((header>>12)&15)!=15) &&          /*bitrate!=15*/
  309.                 (((header>>10)&3)!=3)               /*freqidx!=3*/
  310.               ) {i=1; break;} /*pattern match*/
  311.         }
  312.         if(i) /*found something!*/
  313.         {
  314.             /**check next header (for safer recognition!)**/
  315.             i=((ULONG)mpgbitrate[3-((header>>17)&3)][(header>>12)&15]*144000)/mpgfreq[(header>>10)&3]+((header>>9)&1)-4;
  316.             if((pt+i)>=(ptmax-4)) goto retry_firstheader; /*beyond this buffer!*/
  317.             i=((ULONG)(*(pt+i))<<24)|((ULONG)(*(pt+i+1))<<16)|((ULONG)(*(pt+i+2))<<8)|(ULONG)(*(pt+i+3));
  318.             if( ((i&HDR_CONSTANT)!=(header&HDR_CONSTANT)) ||
  319.                 (((i>>12)&15)==0) ||
  320.                 (((i>>12)&15)==15) ) goto retry_firstheader; /*header mismatch!*/
  321.             /**now we are quite sure that it really is an MPEG frame header**/
  322.             unit->currpt=pt-4;
  323.             unit->currlen=ptmax-pt+4;
  324.             unit->firstheader=header;
  325.             unit->layer=4-((header>>17)&3);
  326.             unit->freqidx=(header>>10)&3;
  327.             unit->mono=( (((header>>6)&3)==MPG_MD_MONO) ? 1 : 0 );
  328.  
  329.             unit->forcemono= unit->layer==2 ? unit->II_forcemono : unit->III_forcemono;
  330.             unit->dacrate= unit->layer==2 ? unit->II_dacrate : unit->III_dacrate;
  331.         }
  332.         else /*not found*/
  333.         {
  334.             ReplyMsg(&unit->currioreq->iomr_Req.io_Message);
  335.             unit->currioreq=NULL; unit->currpt=NULL; unit->currlen=0;
  336.         }
  337.     }
  338.  
  339.  
  340.     if(!unit->prg_pcm)
  341.     {
  342. #ifdef DEBUG
  343. KPutStr("initDelfina...prg_pcm\n");
  344. #endif
  345.         if(!(unit->prg_pcm=Delf_AddPrg(&DSP56K_PCM))) return(1); /*ERROR*/
  346.         Delf_Run(unit->prg_pcm->prog+PROG_PCM_INIT, 0, 0,
  347.                  (ULONG)(unit->forcemono ? 1 : unit->mono),
  348.                  unit->volumeleft,
  349.                  mpgfreq[unit->freqidx],
  350.                  unit->volumeright );
  351.     }
  352.  
  353.     if(unit->layer==2)
  354.     {
  355.         if(!unit->mem_il_mp2) unit->mem_il_mp2=Delf_AllocMem(INTL_MP2_DATL, DMEMF_LDATA|DMEMF_INTERNAL|DMEMF_ALIGN_64);
  356.         if(!unit->mem_ip_mp2) unit->mem_ip_mp2=Delf_AllocMem(INTP_MP2_PROG, DMEMF_PROG|DMEMF_INTERNAL);
  357.         if(!unit->prg_mp2)
  358.         {
  359. #ifdef DEBUG
  360. KPutStr("initDelfina...prg_mp2\n");
  361. #endif
  362.             if(!(unit->prg_mp2=Delf_AddPrg(&DSP56K_MP2))) return(1); /*ERROR*/
  363.             Delf_Run(unit->prg_mp2->prog+PROG_MP2_INIT, 0, 0, unit->mem_il_mp2, unit->mem_ip_mp2, 0, 0);
  364.             Delf_Poke(unit->prg_mp2->ydata+DATY_MP2_FORCEMONO, DMEMF_YDATA, (ULONG)unit->forcemono);
  365.         }
  366.     }
  367.     else if(unit->layer==3)
  368.     {
  369.         if(!unit->mem_il_mp3) unit->mem_il_mp3=Delf_AllocMem(INTL_MP3_DATL, DMEMF_LDATA|DMEMF_INTERNAL|DMEMF_ALIGN_64);
  370.         if(!unit->mem_ip_mp3) unit->mem_ip_mp3=Delf_AllocMem(INTP_MP3_PROG, DMEMF_PROG|DMEMF_INTERNAL);
  371.         if(!unit->mem_x_mp3_ro)
  372.         {
  373.             if(!(unit->mem_x_mp3_ro=Delf_AllocMem(EXTX_MP3_RO,DMEMF_XDATA))) return(1); /*ERROR*/
  374.         }
  375.         if(!unit->mem_y_mp3_quantab)
  376.         {
  377.             if(!(unit->mem_y_mp3_quantab=Delf_AllocMem(EXTY_MP3_QUANTAB,DMEMF_YDATA))) return(1); /*ERROR*/
  378.             Delf_CopyMem(quantab, (void*)unit->mem_y_mp3_quantab, 512*4, DCPF_FROM_AMY|DCPF_YDATA|DCPF_32BIT);
  379.         }
  380.         if(!unit->prg_mp3)
  381.         {
  382. #ifdef DEBUG
  383. KPutStr("initDelfina...prg_mp3\n");
  384. #endif
  385.             if(!(unit->prg_mp3=Delf_AddPrg(&DSP56K_MP3))) return(1); /*ERROR*/
  386.             Delf_Run(unit->prg_mp3->prog+PROG_MP3_INIT, 0, 0, unit->mem_il_mp3, unit->mem_ip_mp3, unit->freqidx, unit->mem_x_mp3_ro);
  387.             Delf_CopyMem(pow43tab, (void*)(unit->prg_mp3->ydata+DATY_MP3_POW43TAB), 8206*4, DCPF_FROM_AMY|DCPF_YDATA|DCPF_32BIT);
  388.             Delf_Poke(unit->prg_mp3->xdata+DATX_MP3_QUANTAB_P, DMEMF_XDATA, unit->mem_y_mp3_quantab);
  389.             Delf_Poke(unit->prg_mp3->ydata+DATY_MP3_FORCEMONO, DMEMF_YDATA, (ULONG)unit->forcemono);
  390.         }
  391.     }
  392.     else return(1); /*ERROR: unsupported layer*/
  393.  
  394.     if(!unit->intkey)
  395.     {
  396. #ifdef DEBUG
  397. KPutStr("initDelfina...AddIntServer\n");
  398. #endif
  399.         unit->delfint.is_Code=(void(*)(void))IntServer;
  400.         unit->delfint.is_Data=unit;
  401.         if(!(unit->intkey=Delf_AddIntServer(unit->prg_pcm->prog+PROG_PCM_INTKEY,&unit->delfint))) return(1); /*ERROR*/
  402.     }
  403.     if(!unit->mod_pcm)
  404.     {
  405. #ifdef DEBUG
  406. KPutStr("initDelfina...AddModule\n");
  407. #endif
  408.         if(!(unit->mod_pcm=Delf_AddModule(DM_Inputs, 0, DM_Outputs, 1,
  409.                             DM_Code, unit->prg_pcm->prog+PROG_PCM_MODULE,
  410.                             DM_Freq, unit->dacrate ? unit->dacrate*1000 : mpgfreq[unit->freqidx],
  411.                             DM_Name, &DevName,
  412.                             0 ))) return(1); /*ERROR*/
  413.     }
  414.  
  415.     unit->initdelf_ok=1;
  416. #ifdef DEBUG
  417. KPutStr("initDelfina...ok\n");
  418. #endif
  419.     return(0);
  420. }
  421.  
  422.  
  423.  
  424. /** called by C_flush() **/
  425. static void cleanupDelfina(struct devunit *unit)
  426. {
  427. #ifdef DEBUG
  428. KPutStr("cleanupDelfina\n");
  429. #endif
  430.     if(unit->mod_pcm) {Delf_RemModule(unit->mod_pcm); unit->mod_pcm=NULL;}
  431.     if(unit->intkey)  {Delf_RemIntServer(unit->intkey); unit->intkey=0;}
  432.     if(unit->prg_mp2) {Delf_RemPrg(unit->prg_mp2); unit->prg_mp2=NULL;}
  433.     if(unit->prg_mp3) {Delf_RemPrg(unit->prg_mp3); unit->prg_mp3=NULL;}
  434.     if(unit->prg_pcm) {Delf_RemPrg(unit->prg_pcm); unit->prg_pcm=NULL;}
  435.     if(unit->mem_il_mp2) {Delf_FreeMem(unit->mem_il_mp2,DMEMF_LDATA|DMEMF_INTERNAL); unit->mem_il_mp2=NULL;}
  436.     if(unit->mem_ip_mp2) {Delf_FreeMem(unit->mem_ip_mp2,DMEMF_PROG |DMEMF_INTERNAL); unit->mem_ip_mp2=NULL;}
  437.     if(unit->mem_il_mp3) {Delf_FreeMem(unit->mem_il_mp3,DMEMF_LDATA|DMEMF_INTERNAL); unit->mem_il_mp3=NULL;}
  438.     if(unit->mem_ip_mp3) {Delf_FreeMem(unit->mem_ip_mp3,DMEMF_PROG |DMEMF_INTERNAL); unit->mem_ip_mp3=NULL;}
  439.     if(unit->mem_x_mp3_ro) {Delf_FreeMem(unit->mem_x_mp3_ro,DMEMF_XDATA); unit->mem_x_mp3_ro=NULL;}
  440.     if(unit->mem_y_mp3_quantab) {Delf_FreeMem(unit->mem_y_mp3_quantab,DMEMF_YDATA); unit->mem_y_mp3_quantab=NULL;}
  441.     unit->initdelf_ok=0;
  442. }
  443.  
  444.  
  445.  
  446. static void __asm IntServer(register __a1 struct devunit *unit)
  447. {
  448.     if(unit->pause || (unit->framebufstate!=FBS_FILLED))
  449.     {
  450.         Delf_Run(unit->prg_pcm->prog+PROG_PCM_MUTE,0,DRUNF_ASYNCH,0,0,0,0);
  451.     }
  452.     else
  453.     {
  454.         if(unit->layer==2)
  455.         {
  456.             if(!Delf_Peek(unit->prg_mp2->ydata+DATY_MP2_BUSY,DMEMF_YDATA))
  457.             {
  458.                 Delf_CopyMem( unit->delfcopypt,
  459.                               (void*)(unit->prg_mp2->xdata+DATX_MP2_INBUF),
  460.                               (ULONG)unit->delfcopysize,
  461.                               DCPF_FROM_AMY|DCPF_XDATA|DCPF_24BIT );
  462.                 Delf_Run( unit->prg_mp2->prog+PROG_MP2_DECODE, 0, DRUNF_ASYNCH,
  463.                           unit->mono,
  464.                           Delf_Peek(unit->prg_pcm->ydata+DATY_PCM_BUFPTR,DMEMF_YDATA),
  465.                           (ULONG)unit->II_translate,
  466.                           (ULONG)unit->II_jsbound );
  467.                 unit->framebufstate=FBS_GETHEADER;
  468.             }
  469.         }
  470.         else if(unit->layer==3)
  471.         {
  472.             if(!Delf_Peek(unit->prg_mp3->ydata+DATY_MP3_BUSY,DMEMF_YDATA))
  473.             {
  474.                 if(unit->bitresok)
  475.                 {
  476.                     Delf_CopyMem( &unit->bitresbuf[0],
  477.                                   (void*)(unit->prg_mp3->xdata+DATX_MP3_INBUF),
  478.                                   (ULONG)(unit->III_main_data_size+33),
  479.                                   DCPF_FROM_AMY|DCPF_XDATA|DCPF_24BIT );
  480.                     Delf_Run( unit->prg_mp3->prog+PROG_MP3_DECODE, 0, DRUNF_ASYNCH,
  481.                               unit->mono,
  482.                               Delf_Peek(unit->prg_pcm->ydata+DATY_PCM_BUFPTR,DMEMF_YDATA),
  483.                               (ULONG)unit->modext,
  484.                               0 );
  485.                 }
  486.                 unit->framebufstate=FBS_GETHEADER;
  487.             }
  488.         }
  489.     }
  490.  
  491.     if(unit->framebufstate==FBS_GETHEADER)
  492.     {
  493.         /*find next valid frame header*/
  494.         ULONG fh=unit->firstheader&HDR_CONSTANT, ch=0;
  495.         while( ((ch&HDR_CONSTANT)!=fh) ||   /*sync, layer, freqidx*/
  496.                (((ch>>12)&15)==0) ||        /*bitrate!=0*/
  497.                (((ch>>12)&15)==15) )        /*bitrate!=15*/
  498.         {
  499.             if(unit->currioreq)
  500.             {
  501.                 if(unit->currlen)
  502.                 {
  503.                     ch=(ch<<8)|(ULONG)(*unit->currpt);
  504.                     unit->currpt++; unit->currlen--;
  505.                 }
  506.                 else
  507.                 {
  508.                     ReplyMsg(&unit->currioreq->iomr_Req.io_Message);
  509.                     unit->currioreq=NULL; unit->currpt=NULL; /*unit->currlen=0;*/
  510.                 }
  511.             }
  512.             else
  513.             {
  514.                 if(!(unit->currioreq=(struct IOMPEGReq*)RemHead(&unit->ioreqlist)))
  515.                 {
  516.                     ch=0; break; /*ERROR*/
  517.                 }
  518.                 unit->currpt=unit->currioreq->iomr_Req.io_Data;
  519.                 unit->currlen=unit->currioreq->iomr_Req.io_Length;
  520.             }
  521.         }
  522.         if(ch) /*found it!*/
  523.         {
  524.             unit->framebufoffset=0;
  525.             unit->framebufleft=((ULONG)mpgbitrate[unit->layer-1][(ch>>12)&15]*144000)/mpgfreq[(ch>>10)&3]+((ch>>9)&1)-4;
  526.             unit->II_translate=mp2translate[unit->freqidx][unit->mono][(ch>>12)&15];
  527.             if(((ch>>6)&3)==MPG_MD_JOINT_STEREO)
  528.             {
  529.                 unit->modext=(ch>>4)&3;
  530.                 unit->II_jsbound=(unit->modext<<2)+4;
  531.             }
  532.             else
  533.             {
  534.                 unit->modext=0;
  535.                 unit->II_jsbound=mp2sblimit[unit->II_translate];
  536.             }
  537.             unit->delfcopysize=unit->framebufleft;
  538.             unit->delfcopypt= &unit->framebuf[0];
  539.             if(!((ch>>16)&1))
  540.             {
  541.                 unit->delfcopysize-=2;
  542.                 unit->delfcopypt+=2;
  543.             }
  544.             unit->framebufstate=FBS_GETFRAMEDATA;
  545.         }
  546.     }
  547.  
  548.     if(unit->framebufstate==FBS_GETFRAMEDATA)
  549.     {
  550.         /*fetch frame data*/
  551. #ifdef DEBUG
  552. KPutChar('i');
  553. #endif
  554.         while(unit->framebufleft)
  555.         {
  556.             if(unit->currioreq)
  557.             {
  558.                 if(unit->currlen)
  559.                 {
  560.                     ULONG i;
  561.                     if(unit->framebufleft<unit->currlen) i=unit->framebufleft;
  562.                     else i=unit->currlen;
  563.                     CopyMem(unit->currpt, &unit->framebuf[unit->framebufoffset], i);
  564.                     unit->currpt+=i; unit->currlen-=i;
  565.                     unit->framebufoffset+=i; unit->framebufleft-=i;
  566.                     if(unit->framebufleft==0) unit->framebufstate=FBS_FILLED;
  567.                 }
  568.                 else
  569.                 {
  570.                     ReplyMsg(&unit->currioreq->iomr_Req.io_Message);
  571.                     unit->currioreq=NULL; unit->currpt=NULL; /*unit->currlen=0;*/
  572.                 }
  573.             }
  574.             else
  575.             {
  576.                 if(!(unit->currioreq=(struct IOMPEGReq*)RemHead(&unit->ioreqlist)))
  577.                 {
  578.                     break; /*ERROR*/
  579.                 }
  580.                 unit->currpt=unit->currioreq->iomr_Req.io_Data;
  581.                 unit->currlen=unit->currioreq->iomr_Req.io_Length;
  582.             }
  583.         }
  584.         if((unit->framebufstate==FBS_FILLED) && (unit->layer==3))
  585.         {
  586.             UWORD md;
  587.             /**extract main data size**/
  588.             gb_pt=(UWORD*)(unit->delfcopypt+2); gb_num=0;
  589.             if(unit->mono)
  590.             {
  591.                 GetBits(9+5+4-16);
  592.                 md=GetBits(12);
  593.             }
  594.             else
  595.             {
  596.                 GetBits(9+3+8-16);
  597.                 md=GetBits(12);
  598.                 GetBits(59-12);
  599.                 md+=GetBits(12);
  600.                 GetBits(59-12);
  601.                 md+=GetBits(12);
  602.             }
  603.             GetBits(59-12);
  604.             md+=GetBits(12);
  605.             unit->III_main_data_size=(md+7)>>3;
  606.             /**bit reservoir handling**/
  607.             {
  608.                 UWORD main_data_begin, i;
  609.                 UBYTE *p0, *p1;
  610.                 p1=unit->delfcopypt;
  611.                 main_data_begin= (UWORD)(*p1)<<1 | (UWORD)(*(p1+1))>>7;
  612.                 /* copy side information */
  613.                 p0= &unit->bitresbuf[0];
  614.                 for(i=32; i>0; i--) *p0++= *p1++;
  615.                 /* copy previous main_data */
  616.                 p0++; /* &unit->bitresbuf[33] */
  617.                 if(unit->bitresoffset>=main_data_begin)
  618.                 {
  619.                     unit->bitresok=1;
  620.                     p1=p0+unit->bitresoffset-main_data_begin;
  621.                     for(i=main_data_begin; i>0; i--) *p0++= *p1++;
  622.                     unit->bitresoffset=main_data_begin;
  623.                 }
  624.                 else
  625.                 {
  626.                     unit->bitresok=0; /* not enough data in reservoir */
  627.                     p0+=unit->bitresoffset;
  628.                 }
  629.                 /* copy current main_data */
  630.                 i=(unit->mono) ? 17 : 32; /* side info length */
  631.                 p1=unit->delfcopypt+i;    /* begin of main_data area */
  632.                 i=unit->delfcopysize-i;   /* mainslots */
  633.                 unit->bitresoffset+=i;
  634.                 for(; i>0; i--) *p0++= *p1++;
  635.             }
  636.         }
  637.     }
  638. }
  639.  
  640.  
  641.  
  642. static UWORD GetBits(UWORD num)
  643. {
  644.     UWORD val=0;
  645.     for(; num>0; num--)
  646.     {
  647.         if(gb_num==0)
  648.         {
  649.             gb_buf= *gb_pt++;
  650.             gb_num= 16;
  651.         }
  652.         gb_num--;
  653.         val+= val;
  654.         val|= (gb_buf>>gb_num)&1;
  655.     }
  656.     return(val);
  657. }
  658.